package cn.com.open.itembank.logic.questiontype;

import cn.com.open.itembank.common.Function;
import cn.com.open.itembank.dto.QuestionImportModel;
import cn.com.open.itembank.dto.QuestionModel;

import java.util.List;

/**
 * Created by lucky on 2017/12/19.
 */
public class MatchItem extends QuestionTypeBase {
    /**
     * Instantiates a new Match item.
     *
     * @param model the model
     */
    public MatchItem(QuestionImportModel model) {
        super(model);
        super.mustHasChoice = true;
    }

    //region 校验子题数是否匹配

    /**
     * 校验填空数与答案数是否匹配.
     *
     * @return the boolean
     */
    @Override
    protected boolean checkQuestionCount() {
        boolean result = true;
        List<QuestionImportModel> subItemsList = importModel.getSubItems();
        for(QuestionImportModel item : subItemsList) {
            int mainContent = item.getPrompt().split("##").length;

            if (mainContent != item.getAnswer().size()) {
                result = false;
                item.setErrorInfo("空数与答案数不匹配；");
            }
        }
        return result;
    }
    //endregion

    //region 校验答案格式

    /**
     * 校验答案格式.
     *
     * @return the boolean
     */
    @Override
    protected boolean checkAnswerFormat() {
        boolean result = true;
        for (QuestionImportModel item : importModel.getSubItems()){
            for (String answer : item.getAnswer()) {
                if (answerIdList.indexOf(answer.trim().toUpperCase()) < 0) {
                    importModel.setErrorInfo("答案格式不正确；");
                    return false;
                }
            }
        }
        return result;
    }
    //endregion

    //region 获取保存模型

    /**
     * 获取保存模型.
     *
     * @return the input question model
     */
    @Override
    public QuestionModel getSaveModel() {
        QuestionModel model = super.getSaveModel();

        for (String choice : importModel.getChoices()) {
            model.getI6().add(choice);
        }

        for (QuestionImportModel sub : importModel.getSubItems()) {
            QuestionModel subModel = new QuestionModel();
            subModel.setI2(sub.getPrompt());
            subModel.setI10(sub.getQuestionAnalysis());

            //I6,I7 需要各种题型重载完成
            for (String choice : sub.getChoices()) {
                subModel.getI6().add(choice);
            }
            //answer存的是abcd，i7需要0123
            for (String answer : sub.getAnswer()) {
                subModel.getI7().add(String.valueOf(Function.getUpperCaseIndex(answer)));
            }

            model.getSub().add(subModel);
        }

        //I17 子试题个数 需要各种题型重载完成
        model.setI17(importModel.getSubItems().size());
        return model;
    }
    //endregion
}
